package com.easy.project.service.system.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.easy.project.base.exception.ServiceException;
import com.easy.project.base.result.SystemErrorCodeEnum;
import com.easy.project.constant.MenuTypeEnum;
import com.easy.project.convert.MenuDomainConvert;
import com.easy.project.domain.dto.system.MenuEditDTO;
import com.easy.project.domain.po.system.SysMenuPO;
import com.easy.project.domain.po.system.SysRolePO;
import com.easy.project.domain.po.system.SysUserPO;
import com.easy.project.domain.vo.system.SysMenuVO;
import com.easy.project.mapper.system.SysMenuMapper;
import com.easy.project.service.system.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.project.util.SaTokenUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.easy.project.base.result.SystemErrorCodeEnum.DATA_FIND_ERROR;

/**
 * <p>
 * 系统菜单表 服务实现类
 * </p>
 *
 * @author wxz
 * @since 2024-01-10
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenuPO> implements ISysMenuService {

    @Resource
    private MenuDomainConvert menuDomainConvert;

    @Override
    public List<SysMenuVO> getMenuByRoleId(List<Long> roleIdList) {
        List<SysMenuPO> sysMenuPOList = baseMapper.getMenuByRoleId(roleIdList);
        return menuDomainConvert.poListToVoList(sysMenuPOList);
    }

    @Override
    public List<SysMenuVO> allMenu() {
        return menuDomainConvert.poListToVoList(this.list());
    }

    @Override
    public List<String> allPath() {
        return allMenu().stream().filter(e -> Objects.equals(e.getMenuType(), MenuTypeEnum.INTERFACE.getCode())).map(SysMenuVO::getMenuRouter).collect(Collectors.toList());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void menuEdit(MenuEditDTO menuEditDTO) {
        Long id = menuEditDTO.getId();
        SysMenuPO sysMenuPO;
        if (Objects.isNull(id)) {
            // 新增操作
            sysMenuPO = menuDomainConvert.editDtoToPo(menuEditDTO);
        } else {
            // 编辑操作
            sysMenuPO = getById(id);
            menuDomainConvert.editDtoToPo(menuEditDTO, sysMenuPO);
        }
        // 验证数据
        validate(sysMenuPO);
        // 存储数据
        saveOrUpdate(sysMenuPO);
    }

    // 验证数据
    private void validate(SysMenuPO sysMenuPO){
        // 菜单未找到
        if (Objects.isNull(sysMenuPO)){
            throw new ServiceException(DATA_FIND_ERROR);
        }
        // 判断菜单编码是否重复
        List<SysMenuPO> menuList = baseMapper.getMenuListByCode(sysMenuPO);
        if (CollectionUtil.isNotEmpty(menuList)){
            throw new ServiceException(SystemErrorCodeEnum.ARGUMENT_VALID_ERROR.getCode(),"菜单编码不能重复!");
        }
    }

}
